﻿#region

using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

#endregion

namespace MUIExt.MUIExt
{
    [ToolboxItem(false)]
    public sealed class MUIExt : WebPart
    {
        // Visual Studio peut mettre à jour automatiquement ce chemin lorsque vous modifiez l'élément de projet Composant Visual Web Part.
        private const string AscxPath = @"~/_CONTROLTEMPLATES/MUIExt/MUIExt/MUIExtUserControl.ascx";

        public MUIExt()
        {
            AllowMinimize = false;
            AllowHide = false;
            AllowClose = false;

            // Default Values
            MUIExtConfiguration = "<Webparts></Webparts>";
        }

        [WebBrowsable(false), WebDisplayName("Configuration"), WebDescription(""),
         Personalizable(PersonalizationScope.Shared), Category("MUIExt")]
        public String MUIExtConfiguration { get; set; }

        protected override void CreateChildControls()
        {
            var control = Page.LoadControl(AscxPath) as MUIExtUserControl;
            if (control == null) throw new Exception("control is null");
            Controls.Add(control);

            control.MUIExt = this;
            control.Initialize(SPContext.Current.Web.SupportedUICultures);
        }

        protected override void OnPreRender(EventArgs e)
        {
            // Create WebPartManager
            var webPartManager = WebPartManager.GetCurrentWebPartManager(Page);
            if (webPartManager == null) throw new Exception("webPartManager is null");

            // Hide this webpart if in BrowseDisplayMode and if not in EnableInputFieldLabels
            Hidden = webPartManager.DisplayMode == WebPartManager.BrowseDisplayMode &&
                !SPContext.Current.FormContext.EnableInputFieldLabels;

            base.OnPreRender(e);
        }

        public void SaveProperties()
        {
            SetPersonalizationDirty();
        }
    }
}